﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Configuration;
using YLPublicService.TransferModel;
using YLPublicService.EnumExtension;

namespace YLPublicService
{
    public partial class UserService
    {
        /// <summary>
        /// 添加工作项包括以下内容：
        /// 添加一个物理文件夹
        /// 在数据库添加一条状态是正在上传的工作项
        /// </summary>
        /// <param name="item"></param>
        /// <param name="guid"></param>
        /// <returns></returns>
        public int AddWorkItem(TransferModel.WorkItem item, Guid guid)
        {
            if (!isOnline(guid))
            {
                return 0;
            }
            var user = getCurrentUser(guid);
            try
            {
                var tpltItemDetial = TransferModel.TemplateItem.TemplateDetial.FromXML(item.Detial);

                //添加工作项Ftp目录
                var dirTitle = global::Common.toDirString(item.DBTime);

                var path = settingManager.AddWorkItemDir(user.LoginName, dirTitle, tpltItemDetial.mName);
                var status1 = dbContext.WorkItemStatus
                    .Where(s => s.status == TransferModel.WorkItemStatus.Uploading.GetIndex()).First();
                WorkItemV2 wi = BuildWorkItem(item, path, status1);
                dbContext.WorkItemV2s.InsertOnSubmit(wi);
                dbContext.SubmitChanges();
                UsersItemV2 ui = new UsersItemV2
                {
                    ItemID = wi.ItemID,
                    UserID = user.UserID
                };
                dbContext.UsersItemV2s.InsertOnSubmit(ui);
                dbContext.SubmitChanges();
                return wi.ItemID;
            }
            catch
            {
                return 0;
            }
        }

        private WorkItemV2 BuildWorkItem(TransferModel.WorkItem item, string path, WorkItemStatus status1)
        {
            WorkItemV2 wi = new WorkItemV2
            {
                Detial = item.Detial,
                TemplateID = item.TemplateID,
                DBTime = DateTime.Now,
                PhotoServerRelativePath = path,
                Status = status1.status
            };
            return wi;
        }
        public bool UpdatedWorkItemStatus(TransferModel.WorkItem item, TransferModel.WorkItemStatus newStatus, Guid guid)
        {
            if (!isOnline(guid))
            {
                return false;
            }
            var user = getCurrentUser(guid);
            var items = dbContext.WorkItemV2s.Where(_item =>
                _item.ItemID == item.WorkItemID);
            if (items.Count() == 1)
            {
                var i = items.First();
                i.Status = newStatus.GetIndex();
                dbContext.SubmitChanges(ConflictMode.ContinueOnConflict);
                return true;
            }
            else
                return false;
        }
        /// <summary>
        /// 获取最近1个月的工单
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public DateTime[] GetUserWorkItemDates(Guid guid)
        {
            if (!isOnline(guid))
            {
                return null;
            }
            var user = getCurrentUser(guid);
            var lastMonthToday = DateTime.Now.Subtract(new TimeSpan(30, 0, 0, 0));
            var ret = from i in dbContext.View_UserItemDates
                      where i.UserID == user.UserID && i.DBTime > lastMonthToday
                      select i.DBTime;
            return ret.ToArray();
        }
        public TransferModel.WorkItem[] GetFinishWorkItems(DateTime start, DateTime end, Guid guid)
        {
            if (!isOnline(guid))
            {
                return null;
            }
            User user = getCurrentUser(guid);
            var x = from i in dbContext.View_FinishWorkItems
                    select new TransferModel.WorkItem
                    {
                        TemplateID = i.TemplateID,
                        Detial = i.Detial,
                        Status = i.Status.ToEnum<TransferModel.WorkItemStatus>(),
                        DBTime = i.DBTime
                    };
            return x.ToArray();
        }

        public IEnumerable<TransferModel.WorkItem> GetWorkItems(Guid guid)
        {
            return getWorkItems<TransferModel.WorkItem>(
                ui => true,
                ui =>
                    new TransferModel.WorkItem
                    {
                        WorkItemID = ui.ItemID,
                        TemplateID = ui.TemplateID,
                        Detial = ui.Detial,
                        Status = ui.Status.ToEnum<TransferModel.WorkItemStatus>(),
                        DBTime = ui.DBTime
                    });
        }
        public IEnumerable<TransferModel.WorkItem> GetWorkItems(DateTime start, DateTime end, Guid guid)
        {
            return getWorkItems<TransferModel.WorkItem>(
                    ui => ui.DBTime >= start && ui.DBTime <= end,
                    ui =>
                        new TransferModel.WorkItem
                        {
                            WorkItemID = ui.ItemID,
                            TemplateID = ui.TemplateID,
                            Detial = ui.Detial,
                            Status = ui.Status.ToEnum<TransferModel.WorkItemStatus>(),
                            DBTime = ui.DBTime
                        });
        }

    }
}